﻿@page "{Id}"

@using System.Globalization
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.Extensions.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Card
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
@using Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.TuiEditor
@using Volo.CmsKit.Blogs
@using Volo.CmsKit.Admin.Web.Pages
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpPageToolbar
@using Volo.CmsKit.Admin.Web.Menus
@using Volo.CmsKit.Admin.Web.Pages.CmsKit.BlogPosts
@using Volo.CmsKit.Admin.Web.Pages.CmsKit.Tags.Components.TagEditor
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Uppy
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Slugify
@using Volo.Abp.Data
@using Volo.Abp.Localization
@using Volo.Abp.ObjectExtending
@inject IStringLocalizerFactory StringLocalizerFactory

@inherits CmsKitAdminPageBase

@model UpdateModel

@{
    PageLayout.Content.Title = L["BlogPosts"].Value;
    PageLayout.Content.BreadCrumb.Add(L["Menu:CMS"].Value);
    PageLayout.Content.MenuItemName = CmsKitAdminMenus.BlogPosts.BlogPostsMenu;
}

@section scripts {
    <abp-script-bundle>
        <abp-script type="typeof(TuiEditorScriptContributor)"/>
        <abp-script type="typeof(UppyScriptContributor)"/>
        <abp-script type="typeof(SlugifyScriptContributor)"/>
        <abp-script src="/client-proxies/cms-kit-common-proxy.js"/>
        <abp-script src="/client-proxies/cms-kit-admin-proxy.js"/>
        <abp-script src="/Pages/CmsKit/BlogPosts/update.js"/>
        <abp-script src="/Pages/CmsKit/Contents/addWidgetModal.js" />
    </abp-script-bundle>
}

@section styles {
    <abp-style-bundle>
        <abp-style type="typeof(TuiEditorStyleContributor)"/>
        <abp-style src="/Pages/CmsKit/BlogPosts/update.css"/>
    </abp-style-bundle>
}

@section content_toolbar {
    @await Component.InvokeAsync(typeof(AbpPageToolbarViewComponent), new { pageName = typeof(UpdateModel).FullName })
}

<abp-card>
    <abp-card-header title="@L["Edit"].Value"></abp-card-header>
    <abp-card-body>

        <div class="mb-3">
            <div id="CurrentCoverImageArea">
                @if (Model.ViewModel.CoverImageMediaId != null)
                {
                    <img height="120" src="/api/cms-kit/media/@Model.ViewModel.CoverImageMediaId"/>
                    <br/>
                    <abp-button button-type="Link" type="button" text="@L["RemoveCoverImage"].Value" id="button-remove-cover-image"/>
                    <br/>
                }
            </div>
            <label class="form-label">@L["CoverImage"]<span class="badge text-bg-light">16:9</span></label>
            <input type="file" id="BlogPostCoverImage" class="form-control" accept="image/*" />
        </div>

        <form asp-page="/CmsKit/BlogPosts/Update" id="form-blog-post-update">
            <abp-input asp-for="Id" />
            <abp-input asp-for="ViewModel.ConcurrencyStamp"  type="hidden" />
            <abp-input asp-for="ViewModel.CoverImageMediaId" type="hidden" />
            
            <abp-input asp-for="ViewModel.Title" />

            <abp-input asp-for="ViewModel.Slug" />

            <abp-input asp-for="ViewModel.ShortDescription" />

            <abp-input asp-for="ViewModel.Content" type="hidden" />

            <div class="content-editor cms-kit-editor"
                 id="ContentEditor"
                 data-input-id="@Html.IdFor(x => x.ViewModel.Content)"
                 data-language="@(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)">
            </div>

            @foreach (var propertyInfo in ObjectExtensionManager.Instance.GetProperties<UpdateModel.UpdateBlogPostViewModel>())
            {
                if (!propertyInfo.Name.EndsWith("_Text"))
                {
                    if (propertyInfo.IsEnum() || !propertyInfo.Lookup.Url.IsNullOrEmpty())
                    {
                        if (propertyInfo.IsEnum())
                        {
                            Model.ViewModel.ExtraProperties.ToEnum(propertyInfo.Name, propertyInfo.Type);
                        }
                        <abp-select asp-for="ViewModel.ExtraProperties[propertyInfo.Name]"
                                    enum-type="@propertyInfo.IsEnum() ? propertyInfo.Type : null"
                                    label="@propertyInfo.GetLocalizedDisplayName(StringLocalizerFactory)"
                                    autocomplete-api-url="@propertyInfo.Lookup.Url"
                                    autocomplete-selected-item-name="@Model.ViewModel.GetProperty(propertyInfo.Name+"_Text")"
                                    autocomplete-selected-item-value="@Model.ViewModel.GetProperty(propertyInfo.Name)"
                                    autocomplete-filter-param-name="@propertyInfo.Lookup.FilterParamName"
                                    autocomplete-items-property-name="@propertyInfo.Lookup.ResultListPropertyName"
                                    autocomplete-display-property-name="@propertyInfo.Lookup.DisplayPropertyName"
                                    autocomplete-value-property-name="@propertyInfo.Lookup.ValuePropertyName"></abp-select>
                    }
                    else
                    {
                        <abp-input type="@propertyInfo.GetInputType()"
                                   asp-for="ViewModel.ExtraProperties[propertyInfo.Name]"
                                   label="@propertyInfo.GetLocalizedDisplayName(StringLocalizerFactory)"
                                   asp-format="@propertyInfo.GetInputFormatOrNull()"
                                   value="@propertyInfo.GetInputValueOrNull(Model.ViewModel.GetProperty(propertyInfo.Name))" />
                    }
                }
            }
            
        </form>

        @if (Model.TagsFeature?.IsEnabled == true)
        {
            <hr />

            @await Component.InvokeAsync(typeof(TagEditorViewComponent), new
            {
                entityType = BlogPostConsts.EntityType,
                entityId = Model.Id.ToString(),
                displaySubmitButton = false
            })
        }
    </abp-card-body>
    <abp-card-footer>
        <abp-button button-type="Primary" type="submit" text="@L["Submit"].Value" id="button-blog-post-update" />
    </abp-card-footer>
</abp-card>
<input hidden id="GeneratedWidgetText" />